layui.config({
	base : "js/"
}).use(['form','layer','jquery','upload'],function(){
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		layedit = layui.layedit,
		laydate = layui.laydate,
        upload = layui.upload,
		$ = layui.jquery;

    //查询信息分类
    $.get("/admin/queryCategoryByType/3",function (msg) {
        if(msg.code == 0){
            var str = "";
            var data = msg.data;
            for(var i=0; i<data.length; i++){
                str += '<option value="'+data[i]['categoryId']+'">'+data[i]['name']+'</option>';
            }
            $("#categoryId").append(str);
        }else{
            layer.alert("加载信息分类出错");
        }
        form.render('select');
    });

    //实例化上传文件框
    upload.render({
        elem: '#upload'
        ,url: '/uploadFile' //上传接口
        ,done: function(res){
            $("#imgBox").append("<a target='_blank' href='"+res.data.src+"'><img src='" + res.data.src + "'>");
            $("#imgBox").append("<input type='hidden' name='imgUrl' class='imgUrl' value='" + res.data.src + "'>");
        }
    });
 	form.on("submit(addLinks)",function(data){
 	    console.log(data);
 	    debugger;
        $.post({
            url : "/admin/addMessage",
            data : JSON.stringify(data.field),
            contentType : "application/json;charset=utf-8", //必须，告诉服务器这是json
            success : function(resp){
                //layer.close(index);
                if(resp.code == '0'){
                    if(data.field.messageId==''){
                        parent.layer.msg('添加成功', {icon: 1});
                    } else {
                        parent.layer.msg('更新成功', {icon: 1});
                    }
                    layer.closeAll('iframe');
                }else{
                    layer.msg(resp.msg);
                }
            }
        });
 		return false;
 	})
	
})
